/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.CpAppInfo;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;

public class CpAppInfoServiceImpl extends AbstractService implements CpAppInfoService
{
	private Logger logger;
	
	public CpAppInfoServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.BOSSSECTION, Config.KEY_DBCP_NAME);
	}
	
	@Override
	public CpAppInfo getCpAppInfo(int id) 
	{
		/*String sql = "select * from cpappinfo where id=" + String.valueOf(id) + "";
		ModelGenerator<CpAppInfo> dbcpmg = new ModelGenerator<CpAppInfo>(CpAppInfo.class);
		return dbcpmg.generate(dbcp, sql);*/
		return null;
	}

	@Override
	public List<CpAppInfo> getCpAppInfos()
	{
		String sql = "select * from t_appinfo order by createdate";
		logger.debug("sql: " + sql);
		ModelGenerator<CpAppInfo> dbcpmg = new ModelGenerator<CpAppInfo>(CpAppInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public List<CpAppInfo> getCpAppInfos(int cpid) {
		String sql = "select * from t_appinfo where cpid=" + String.valueOf(cpid) + " order by createdate";
		logger.debug("sql: " + sql);
		ModelGenerator<CpAppInfo> dbcpmg = new ModelGenerator<CpAppInfo>(CpAppInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	@Override
	public boolean saveCpAppInfo(Account operator, String type, CpAppInfo cpapp)
	{
		/*if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			cpapp.setId(getTableId("cpappinfo"));
//			appmd5 varchar(128),
//			appiconname varchar(128),
//			reiconname varchar(128),
//			appiconsrc varchar(512),
//			appfilename varchar(128),
//			refilename varchar(128),
//			appfilesrc varchar(512),
//			appentry varchar(128),
			
			sql = 
				"insert into cpappinfo(id,cpid,appnumber,appname,apppackage,appstatus,createuser,createdate,appversion" +
				",appmd5,appiconname,reiconname,appiconsrc,appfilename,refilename,appfilesrc,appentry,channelid,projectid) " +
				"values(" + String.valueOf(cpapp.getId()) + ",'" + cpapp.getCpid() + "','" + 
				encodeDb(cpapp.getAppnumber()) + "','" + encodeDb(cpapp.getAppname()) + "','" +
						encodeDb(cpapp.getApppackage()) + "','0','" + cpapp.getCreateuser()+ "','" +
						cpapp.getCreatedate() + "','" + cpapp.getAppversion()+ "'"+
						",'" + cpapp.getAppmd5()+ "'"+
						",'" + cpapp.getAppiconname()+ "'"+
						",'" + cpapp.getReiconname()+ "'"+
						",'" + cpapp.getAppiconsrc()+ "'"+
						",'" + cpapp.getAppfilename()+ "'"+
						",'" + cpapp.getRefilename()+ "'"+
						",'" + cpapp.getAppfilesrc()+ "'"+
						",'" + cpapp.getAppentry()+ "'"+
						",'" + cpapp.getChannelid()+ "'"+
						",'" + cpapp.getProjectid()+ "'"+
						
						")";
		}
		else if (type.equalsIgnoreCase("update"))
		{
			sql = 
				"update cpappinfo set appname='" + encodeDb(cpapp.getAppname()) + "'," +
						"apppackage='" + encodeDb(cpapp.getApppackage()) + "', " +
						"appversion='" + encodeDb(cpapp.getAppversion()) + "' " +
						
						",appmd5='" + encodeDb(cpapp.getAppmd5()) + "' " +
						",appiconname='" + encodeDb(cpapp.getAppiconname()) + "' " +
						",reiconname='" + encodeDb(cpapp.getReiconname()) + "' " +
						",appiconsrc='" + encodeDb(cpapp.getAppiconsrc()) + "' " +
						",appfilename='" + encodeDb(cpapp.getAppfilename()) + "' " +
						",refilename='" + encodeDb(cpapp.getRefilename()) + "' " +
						",appfilesrc='" + encodeDb(cpapp.getAppfilesrc()) + "' " +
						",appentry='" + encodeDb(cpapp.getAppentry()) + "' " +
						",channelid='" +cpapp.getChannelid() + "' " +
						",projectid='" + cpapp.getProjectid() + "' " +
						
				" where id=" + cpapp.getId() + "";
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "update cpappinfo set appstatus='1' where id=" + String.valueOf(cpapp.getId()) + "";
		}
		else if (type.equalsIgnoreCase("online"))
		{
			sql = "update cpappinfo set appstatus='0' where id=" + String.valueOf(cpapp.getId()) + "";
		}
		else if (type.equalsIgnoreCase("offline"))
		{
			sql = "update cpappinfo set appstatus='1' where id=" + String.valueOf(cpapp.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				CpAppInfo lo = null;
				if (!type.equals("delete"))
					lo = getCpAppInfo(cpapp.getId());
				logTableOperate("cpappinfo", type, operator, lo);
				return true;
			}
		}	*/	
		return false;
	}

	@Override
	public List<CpAppInfo> getCpAppInfoByContent(int cpid, int channelid,
			String appname) {
		String sql = "select * from t_appinfo where 1=1";
		if(cpid>0)
		{
			sql+=" and cpid='"+cpid+"' ";
		}
		if(appname.length()>0)
		{
			sql+=" and find_in_set('"+appname+"',appname)>0 ";
		}
		if(channelid>0)
		{
			sql+=" and appnumber in (select appnumber from channelappinfo) ";
		}
		sql+=" order by createdate";
		logger.debug("sql: " + sql);
		ModelGenerator<CpAppInfo> dbcpmg = new ModelGenerator<CpAppInfo>(CpAppInfo.class);
		return dbcpmg.generateList(dbcp, sql);
	}

}
